package com.sheledon.flyCat.core.handler;


import com.sheledon.flyCat.common.pojo.MethodDetail;
import com.sheledon.flyCat.core.factory.HttpResponseFactory;
import com.sheledon.flyCat.core.factory.RequestHandlerFactory;
import com.sheledon.flyCat.core.factory.RequestRouteMapper;
import com.sheledon.flyCat.network.socket.http.HttpRequest;
import com.sheledon.flyCat.network.socket.http.HttpResponse;


/**
 * @author sheledon
 */
public class DispatcherHandler {
    public static HttpResponse dispatch(HttpRequest request){
        MethodDetail methodDetail = RequestRouteMapper.getMethodDetail(request.getMethod(),request.getUrl());
        methodDetail.setJson(request.getBody());
        methodDetail.setQueryParamsMap(request.getRequestParams());
        RequestHandler handler = RequestHandlerFactory.getRequestHandler(request.getMethod());
        if (handler==null){
            return HttpResponseFactory.getErrorResponse();
        }
        return  handler.handle(methodDetail);
    }
}
